package cn.project.controller;
import cn.project.pojo.Message;
import cn.project.pojo.chat;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.security.Principal;

@Controller
public class GreetingController {
    @Resource
    SimpMessagingTemplate messagingTemplate;
    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Message greeting(Message message)throws Exception{
            return message;
    }
    /**
     * Principal 可以获取到SpringSecurity登录的信息
     */
    @MessageMapping("/getxx")
    public void chat(Principal principal, chat chat){
        String from=principal.getName();
        chat.setFrom(from);
        messagingTemplate.convertAndSendToUser(chat.getTo(),"/queue/messge",chat);
    }
}
